macroScript LevelExport category:"AirCrash"
(
	select $box*
	
	output_name = getSaveFileName caption:"XML Level File" types:"Level (*.lvl)|*.lvl|"

	if output_name != undefined then 
	(
		output_file = createfile output_name

		format "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" to:output_file
		format "<SCENE>\n" to:output_file

		for obj in selection do
		(
			length = obj.length
			width = obj.width
			height = obj.height
			posx = obj.pos.x
			posy = obj.pos.z
			posz = obj.pos.y
			physX = getUserProp obj "physx"
			diffMap = obj.material.diffuseMap.fileName
			arr = filterString diffMap "\\"
			diffMap = arr[arr.count]
			normMap = "NONE"
			specMap = "NONE"
			name = getUserProp obj "Name"
			if name == undefined then
			(
				name = "NONAME"
			)
			
			if obj.material.bumpMap != undefined then
			(
				normMap = obj.material.bumpMap.fileName
				arr2 = filterString normMap "\\"
				normMap = arr2[arr2.count]
			)
			if obj.material.specularLevelMap != undefined then
			(
				specMap = obj.material.specularLevelMap.fileName
				arr3 = filterString specMap "\\"
				specMap = arr3[arr3.count]
				
			)
			
			format "\t<BOX name=\"%\" width=\"%\" length=\"%\" height=\"%\" posx=\"%\" posy=\"%\" posz=\"%\">\n" name width length height posx posy posz to:output_file
			format "\t\t<PHYSX state=\"%\"></PHYSX>\n" physx to:output_file
			format "\t\t<MAPS diffuse=\"%\" normal=\"%\" specular=\"%\"></MAPS>\n" diffMap normMap specMap to:output_file
			format "\t</BOX>\n" to:output_file
		)
		
		select $ForceField*
		
		for obj in selection do
		(
			length = obj.length
			width = obj.width
			height = obj.height
			posx = obj.pos.x
			posy = obj.pos.z
			posz = obj.pos.y
			velx = getUserProp obj "velx"
			vely = getUserProp obj "vely"
			velz = getUserProp obj "velz"
			name = getUserProp obj "Name"
			if name == undefined then
			(
				name = "NONAME"
			)
			
			format "\t<FORCEFIELD name=\"%\" width=\"%\" length=\"%\" height=\"%\" posx=\"%\" posy=\"%\" posz=\"%\">\n" name width length height posx posy posz to:output_file
			format "\t\t<VELOCITY x=\"%\" y=\"%\" z=\"%\"></VELOCITY>\n" velx vely velz to:output_file
			format "\t</FORCEFIELD>\n" to:output_file
		)
		
		select $Trigger*
		
		for obj in selection do
		(
			length = obj.length
			width = obj.width
			height = obj.height
			posx = obj.pos.x
			posy = obj.pos.z
			posz = obj.pos.y
			name = getUserProp obj "Name"
			if name == undefined then
			(
				name = "NONAME"
			)
			
			format "\t<TRIGGER name=\"%\" width=\"%\" length=\"%\" height=\"%\" posx=\"%\" posy=\"%\" posz=\"%\"></TRIGGER>\n" name width length height posx posy posz to:output_file
		)
		
		format "</SCENE>" to:output_file
		close output_file
		print "DONE"
	)
	deselect $*
)